package com.gide;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;

import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;


public class OSSUtil {

	@Autowired
	private OSSConfig ossConfig;
	
	/**
	 * 从oss获取图片数据
	 * @param url 图片url地址
	 * @param resize 图片缩放比例 1-100
	 * @return 图片二进制数据
	 */
	public byte[] getObjectDataByUrl(String url,int resize) {
		
		String objectName = url.replace(ossConfig.getPublicImageBaseUrl(), "");
		objectName = objectName.replace(ossConfig.getPrivateImageBaseUrl(), "");
		
		return this.getObjectData(objectName, resize);
		
	}
	
	/**
	 * 从oss获取公开访问权限图片数据
	 * @param objectName 图片在oss bucket中的名字
	 * @param resize 图片缩放比例 1-100
	 * @return 图片二进制数据
	 */
	public byte[] getPublicObjectData(String objectName, int resize) {

		byte[] result = new byte[0];

		OSSClient client = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(),ossConfig.getAccessKeySecret());

		String style = "image/resize,p_" + String.valueOf(resize);

		GetObjectRequest request = new GetObjectRequest(ossConfig.getPublicBucket(), objectName);
		request.setProcess(style);

		OSSObject object = client.getObject(request);

		InputStream iStream = object.getObjectContent();

		byte[] buf = new byte[1024];
		int n = 0;
		while (n != -1) {

			try {
				n = iStream.read(buf, 0, buf.length);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (n == -1) {
				break;
			}

			byte[] temp = result;

			result = new byte[temp.length + n];

			System.arraycopy(temp, 0, result, 0, temp.length);
			System.arraycopy(buf, 0, result, temp.length, n);

		}

		try {
			iStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		client.shutdown();

		return result;

	}

	/**
	 * 从oss获取图片数据
	 * @param objectName 图片在oss bucket中的名字
	 * @param resize 图片缩放比例 1-100
	 * @return 图片二进制数据
	 */
	public byte[] getObjectData(String objectName, int resize) {

		byte[] result = new byte[0];

		OSSClient client = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(),ossConfig.getAccessKeySecret());

		String style = "image/resize,p_" + String.valueOf(resize);

		GetObjectRequest request = new GetObjectRequest(ossConfig.getBucket(), objectName);
		request.setProcess(style);

		OSSObject object = client.getObject(request);

		InputStream iStream = object.getObjectContent();

		byte[] buf = new byte[1024];
		int n = 0;
		while (n != -1) {

			try {
				n = iStream.read(buf, 0, buf.length);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (n == -1) {
				break;
			}

			byte[] temp = result;

			result = new byte[temp.length + n];

			System.arraycopy(temp, 0, result, 0, temp.length);
			System.arraycopy(buf, 0, result, temp.length, n);

		}

		try {
			iStream.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		client.shutdown();

		return result;

	}

	/**
	 * 获取图片在oss中的授权访问url
	 * @param objectName 图片在oss bucket中的名字
	 * @param resize 图片的缩放比例 1-100
	 * @param expiration url过期时间
	 * @return 临时授权的url访问地址
	 */
	public String getObjectUrl(String objectName, int resize, long expiration) {

		OSSClient ossClient = new OSSClient(ossConfig.getEndpoint(), ossConfig.getAccessKeyId(),ossConfig.getAccessKeySecret());
		// 图片处理样式
		String style = "image/resize,p_" + String.valueOf(resize);

		// 设置过期时间
		Date expirationDate = new Date(new Date().getTime() + 1000 * 60 * expiration);

		GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(ossConfig.getBucket(), objectName,HttpMethod.GET);

		req.setExpiration(expirationDate);

		req.setProcess(style);

		URL signedUrl = ossClient.generatePresignedUrl(req);

		return signedUrl.toString();

	}

	public OSSConfig getOssConfig() {
		return ossConfig;
	}

	public void setOssConfig(OSSConfig ossConfig) {
		this.ossConfig = ossConfig;
	}
}
